<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\Apply;
use app\admin\model\Package;
use think\Db;
use think\db\exception\BindParamException;
use think\exception\PDOException;

/**
 * 游戏包管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/08/07
 * Class PackageService
 * @package app\admin\service
 */
class PackageService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new Package();
    }
	
	/**
     * 获取数据列表
     * @return array
     * @since 2023/08/07
     * @author ZhangXuanNeng
     */
    public function getList(): array
    {
        $param = request()->param();

        // 查询条件
        $map = [];
        if(isset($param['game_id']) && !empty($param['game_id'])){
            $map[] = ['game_id','=',$param['game_id']];
        }
        if(isset($param['channel_id']) && !empty($param['channel_id'])){
            $map[] = ['channel_id','=',$param['channel_id']];
        }
        if(isset($param['sdk_version']) && !empty($param['sdk_version'])){
            $map[] = ['sdk_version','=',$param['sdk_version']];
        }
        if(isset($param['status'])){
            $map[] = ['status','=',$param['status']];
        }
        if(isset($param['package_name']) && !empty($param['package_name'])){
            $map[] = ['package_name','like','%'.$param['package_name'].'%'];
        }
        if(isset($param['bundle_id']) && !empty($param['bundle_id'])){
            $map[] = ['bundle_id','like','%'.$param['bundle_id'].'%'];
        }
        return parent::getList($map); // TODO: Change the autogenerated stub
    }

    /**
     * 添加或编辑
     * @return array
     * @throws BindParamException
     * @throws PDOException
     * @since 2020/11/19
     * @author ZhangXuanNeng
     */
    public function edit(): array
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = isset($argList[0]) ? $argList[0] : [];
        // 是否打印SQL
        $is_sql = isset($argList[1]) ? $argList[1] : false;
        if (!$data) {
            $data = request()->param();
        }
        if(isset($data['id']) && $data['id'] > 0){
            $package_name = str_replace(array('(安卓版)','(苹果版)','(pc版)','(h5版)','(谷歌版)'),'', $data['package_name']);
            if(strstr($package_name, '(') || strstr($package_name, ')')){
                return message('游戏名称不能包含特殊字符！', false);
            }
            $data['package_name'] = $package_name;
        }else{
            if(strstr($data['package_name'], '(') || strstr($data['package_name'], ')')){
                return message('游戏名称不能包含特殊字符！', false);
            }
        }
        $error = '';
        $rowId = $this->model->edit($data, $error, $is_sql);
        if ($rowId) {
            $this->RsyncGamePackage($rowId);
            return message();
        }
        return message($error, false);
    }

    public function getChannelPackage($admin_id){
        $param = request()->param();
        $map = [];
        if(isset($param['package_name'])){
            $map[] = ['package_name','like','%'.$param['package_name'].'%'];
        }
        return parent::getList($map);
    }
    public function getPackageConfig(){
        $param = request()->param();
        $row = $this->model->getPackageConfig($param['package_id']);
        if($row){
            $row['pay_way'] = explode(',',$row['pay_way']);
            $row['payment_id_config'] = json_decode($row['payment_id_config'],true);
            $row['channel_config'] = json_decode($row['channel_config'],true);
            $row['ad_config'] = json_decode($row['ad_config'],true);
            $row['pay_site'] = json_decode($row['pay_site'],true);
            $row['login_site'] = json_decode($row['login_site'],true);
            $row['register_site'] = json_decode($row['register_site'],true);
        }
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $row,
        );
        return $message;
    }
    public function getPackageChannelConfig(){
        $param = request()->param();
        $row = Db::name('package_channel_config')->where('package_id',$param['package_id'])->find();
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $row,
        );
        return $message;
    }
    public function setPackageConfig(){
        $param = request()->param();
        if(isset($param['pay_way'])){
            $param['pay_way'] = implode(',',$param['pay_way']);
        }
        if(isset($param['payment_id_config'])){
            $param['payment_id_config'] = json_encode($param['payment_id_config']);
        }
        if(isset($param['channel_config'])){
            $param['channel_config'] = json_encode($param['channel_config']);
        }
        if(isset($param['ad_config'])){
            $param['ad_config'] = json_encode($param['ad_config']);
        }
        if(isset($param['register_site'])){
            $param['register_site'] = json_encode($param['register_site']);
        }
        if(isset($param['login_site'])){
            $param['login_site'] = json_encode($param['login_site']);
        }
        if(isset($param['pay_site'])){
            $param['pay_site'] = json_encode($param['pay_site']);
        }
        $row = $this->model->setPackageConfig($param);
        return $row;
    }
    public function setPackageChannelConfig(){
        $param = request()->param();
        $row = $this->model->setPackageChannelConfig($param);
        return $row;
    }
    public function copyMessage(){
        $param = request()->param();
        $row = $this->model->copyMessage($param['package_id']);
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $row,
        );
        return $message;
    }
    public function setPackageChannelApply(){
        $param = request()->param();
        $row = $this->model->setPackageChannelApply($param['package_id']);
        return $row;
    }
    public function getPackageRenewConfig(){
        $param = request()->param();
        $row = $this->model->getPackageRenewConfig($param['package_id']);
        $message = array(
            "msg" => '操作成功',
            "code" => 0,
            "data" => $row,
        );
        return $message;
    }
    public function setPackageRenewConfig(){
        $param = request()->param();
        $row = $this->model->setPackageRenewConfig($param);
        return $row;
    }

    /**
     * 游戏包同步游聚系统
     * $package_id int 游戏包id
    */
    public function RsyncGamePackage($package_id)
    {
        $info = $this->model->where('id', $package_id)->find();
        $yanfa_game_id = Db::name('game')->where('id', $info['game_id'])->value('yanfa_game_id');
        if ($yanfa_game_id) {
            $secret = 'KfMTQ8EfqzFmv8L1K1TaxNds42yy4r3P';
            $apiUrl = Env('requestUrl.rsyncPackageUrl');
            $data = array(
                'pack_id' => $package_id,
                'name' => $info['package_name'],
                'game_id' => $yanfa_game_id
            );
            $str = "game_id={$data['game_id']}&name={$data['name']}&pack_id={$data['pack_id']}{$secret}";
            write_log('INFO', 'PackageService.php', '签名原串:' . $str, 'package_rsync', true);
            $data['sign'] = md5($str);
            write_log('INFO', 'PackageService.php', '发送数据:' . json_encode($data), 'package_rsync', true);
            write_log('INFO', 'PackageService.php', '发送地址:' . $apiUrl, 'package_rsync', true);
            $res = curl_post($apiUrl, $data);
            write_log('INFO', 'PackageService.php', '响应结果:' . $res, 'package_rsync', true);
        }
    }
    // 同步计费点
    public function setProduct(){
        $param = request()->param();
        $row = $this->model->setProduct($param);
        return $row;
    }
    /**
     * 重新打包--该游戏所有分包
     * @param int $data['id'] packageId
     * @return array    返回值描述
     */
    public function setApplyStatus(){
        $data = request()->param();
        $apply = new Apply();
        $info = $this->model->where('id',$data['id'])->find();
        if($info['sdk_version'] == 2 && !empty($info['appstore_url'])){
            return message('商店包勿操作', false);
        }
        if($info['sdk_version'] == 3){
            return message('h5 勿操作', false);
        }
        $map[] = ['status','=',1];
        $map[] = ['package_id','=',$data['id']];
        $map[] = ['sdk_version','in',[1,2,4]];
        $rowId = $apply->where($map)->update(['enable_status'=>2]);
        if ($rowId) {
            return message();
        }
        return message('操作失败', false);
    }
    /**
     * 收款批量配置
     * @param array package_id 操作游戏包id
     * @param array pay_way 支付方式
     * @param array payment_id_config 收款账户配置
     * @param int pay_type 支付类型:1=网页支付,2=渠道支付
     * @return array    返回值描述
     */
    public function batchSPackageConfig(){
        $param = request()->param();
        if(isset($param['pay_way'])){
            $param['pay_way'] = implode(',',$param['pay_way']);
        }
        if(isset($param['payment_id_config'])){
            $param['payment_id_config'] = json_encode($param['payment_id_config']);
        }
        $package_id = $param['package_id'];
        foreach ($package_id as $k=>$v){
            $param['package_id'] = $v;
            $row = $this->model->setPackageConfig($param);
            if($row['code'] != 0){
                return $row;
            }
        }
        return message();
    }
}